package com.veezean.codereview.server.common;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * <类功能简要描述>
 *
 * @author Veezean, 公众号 @架构悟道
 * @since 2023/3/7
 */
@Configuration
@Slf4j
@EnableOpenApi
public class SwaggerConfig {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.OAS_30)
                .globalRequestParameters(defaultHeader())
                .apiInfo(apiInfo())
                //开启
                .enable(true)
                .select()
                //扫描路径下使用@Api的controller
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                //.paths(PathSelectors.regex("/client/\\w+/[^check]\\w+"))
                .build();

    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("CodeReviewServer接口文档")
                .description("")
                //设置作者信息
                .contact(new Contact("veezean","http://blog.codingcoder.cn","veezean@outlook.com"))
                .version("1.0")
                .build();
    }

    private static List<RequestParameter> defaultHeader(){
        RequestParameterBuilder account = new RequestParameterBuilder();
        account.name("account").description("账号").in(ParameterType.HEADER).required(false).build();
        RequestParameterBuilder pwd = new RequestParameterBuilder();
        pwd.name("pwd").description("密码（加密）").in(ParameterType.HEADER).required(false).build();
        List<RequestParameter> pars = new ArrayList<>();
        pars.add(account.build());
        pars.add(pwd.build());
        return pars;
    }
}
